package com.ruoyi.app.controller.member.service.impl;

import com.ruoyi.app.controller.member.entity.BO.MemberVipTypeUpgradeBO;
import com.ruoyi.app.controller.member.mapper.IMemberMapper;
import com.ruoyi.app.controller.member.mapper.IMemberVipBenefitMapper;
import com.ruoyi.app.controller.member.mapper.IMemberVipTypeMapper;
import com.ruoyi.app.controller.member.service.IMemberVipTypeService;
import com.ruoyi.common.constant.MemberConstants;
import com.ruoyi.common.constant.OrderConstants;
import com.ruoyi.common.constant.SysConfigConstants;
import com.ruoyi.common.entity.DO.member.MemberDO;
import com.ruoyi.common.entity.DO.member.MemberVipBenefitDO;
import com.ruoyi.common.entity.DO.member.MemberVipTypeDO;
import com.ruoyi.common.entity.VO.member.MemberVipBenefitVO;
import com.ruoyi.common.entity.VO.member.MemberVipTypeVO;
import com.ruoyi.common.entity.VO.product.PetrolProductVO;
import com.ruoyi.common.exception.BusinessException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class MemberVipTypeServiceImpl implements IMemberVipTypeService {

    @Resource
    private IMemberVipTypeMapper memberVipTypeMapper;

    @Resource
    private IMemberVipBenefitMapper benefitMapper;

    @Resource
    private IMemberMapper memberMapper;

    @Override
    public MemberVipTypeUpgradeBO getUpgradeInfoById(Long vipTypeId, BigDecimal totalRechargeAmount,BigDecimal totalPetrolAmount) {
        MemberVipTypeDO condition = new MemberVipTypeDO();
        condition.setIsEnable(1);
        condition.setIsDelete(0);
        MemberVipTypeUpgradeBO vipTypeUpgradeBO = new MemberVipTypeUpgradeBO();
        vipTypeUpgradeBO.setNowId(vipTypeId);
        MemberVipTypeDO nextVipTypeInfo = memberVipTypeMapper.getNextVipTypeInfo(vipTypeId);
        if(null == nextVipTypeInfo){
            //满级
            vipTypeUpgradeBO.setNextId(vipTypeId);
            vipTypeUpgradeBO.setNextLogo("");
            vipTypeUpgradeBO.setNextName("");
            vipTypeUpgradeBO.setGapValue(BigDecimal.ZERO);
            vipTypeUpgradeBO.setProgressRate(100);
            vipTypeUpgradeBO.setNowValue(totalRechargeAmount.add(totalPetrolAmount));
        }else{
            vipTypeUpgradeBO.setNextId(nextVipTypeInfo.getId());
            vipTypeUpgradeBO.setNextLogo(nextVipTypeInfo.getVipTypeLogo());
            vipTypeUpgradeBO.setNextName(nextVipTypeInfo.getVipTypeName());
            BigDecimal upgradeValue = new BigDecimal(nextVipTypeInfo.getUpgradeValue());
            BigDecimal nowValue = BigDecimal.ZERO;
            switch (MemberConstants.MemberVipTypeConditionEnum.match(nextVipTypeInfo.getUpgradeCondition())){
                case RECHARGE:
                    vipTypeUpgradeBO.setUpgradeDesc("充值");
                    nowValue = totalRechargeAmount;
                    break;
                case PETROL:
                    vipTypeUpgradeBO.setUpgradeDesc("消费");
                    nowValue = totalPetrolAmount;
                    break;
                case RECHARGE_PETROL:
                    vipTypeUpgradeBO.setUpgradeDesc("充值或消费");
                    nowValue = totalPetrolAmount.add(totalRechargeAmount);
                    break;
                default:break;
            }
            if(nowValue.compareTo(upgradeValue) >=0){
                throw new BusinessException("会员等级有变化,请刷新后重试!");
            }else{
                double mtn;
                vipTypeUpgradeBO.setNowValue(nowValue);
                vipTypeUpgradeBO.setGapValue(upgradeValue.subtract(nowValue));
                if (nowValue.equals(BigDecimal.ZERO)) {
                    mtn = 0.00;
                } else {
                    try {
                        mtn = nowValue.multiply(BigDecimal.valueOf(100L)).divide(upgradeValue, 2, BigDecimal.ROUND_HALF_UP).doubleValue();
                    } catch (ArithmeticException e) {
                        mtn = 0.00;
                    }
                }
                vipTypeUpgradeBO.setProgressRate(mtn);
            }
        }
        return vipTypeUpgradeBO;

    }

    @Override
    public void upgradeCheck(Long memberId) {
        List<MemberVipTypeDO> memberVipTypeList = memberVipTypeMapper.searchVipTypeList();
        if(memberVipTypeList.isEmpty()){
            throw new BusinessException("系统初始化会员等级数据异常!");
        }
        MemberDO memberAsset = memberMapper.getAssetById(memberId);
        if(null == memberAsset){
            throw new UnauthorizedException("请重新登录！");
        }

        boolean isBreak=false;
        long nextVipTypeId=0;
        for (MemberVipTypeDO one:memberVipTypeList
        ) {
            switch (MemberConstants.MemberVipTypeConditionEnum.match(one.getUpgradeCondition())){
                case RECHARGE:
                    isBreak = memberAsset.getTotalRechargeAmount().compareTo(BigDecimal.valueOf(one.getUpgradeValue()))>=0;
                    break;
                case PETROL:
                    isBreak = memberAsset.getTotalPetrolAmount().compareTo(BigDecimal.valueOf(one.getUpgradeValue()))>=0;
                    break;
                case RECHARGE_PETROL:
                    BigDecimal total = memberAsset.getTotalPetrolAmount().add(memberAsset.getTotalRechargeAmount());
                    isBreak = total.compareTo(BigDecimal.valueOf(one.getUpgradeValue()))>=0;
                    break;
                default:break;
            }
            if(isBreak){
                nextVipTypeId = one.getId();
                break;
            }
        }
        if(nextVipTypeId>0){
            MemberDO memberUpdate = new MemberDO();
            memberUpdate.setId(memberId);
            memberUpdate.setVipTypeId(nextVipTypeId);
            memberUpdate.setUpdateTime(new Date());
            memberUpdate.setUpdateBy("会员等级变更");
            memberMapper.updateByPrimaryKeySelective(memberUpdate);
        }
    }

    @Override
    public List<MemberVipTypeVO> searchVipBenefitList(Long shopId) {
        List<MemberVipTypeVO> vipTypeBenefitList = memberVipTypeMapper.searchVipTypeForBenefitList();
        for (MemberVipTypeVO one:vipTypeBenefitList
             ) {
            List<MemberVipBenefitVO> vipBenefitList = benefitMapper.searchListByVipTypeId(one.getId(),shopId);
            if(!vipBenefitList.isEmpty()){
                List<String> petrolBenefitDescList = new ArrayList<>();
                List<String> rechargeBenefitDescList = new ArrayList<>();
                List<String> creditBenefitDescList = new ArrayList<>();
                for (MemberVipBenefitVO benefit:vipBenefitList){
                    if(SysConfigConstants.SystemModuleEnum.RECHARGE.getValue() == benefit.getSystemModule()){
                        //充值
                        if(1 == benefit.getIsCredit()){
                            if(benefit.getPrice().compareTo(BigDecimal.ONE)>0){
                                if(benefit.getBenefitCredit()>0) {
                                    rechargeBenefitDescList.add("充值" + benefit.getPrice().intValue() + "元，赠送" + benefit.getBenefitCredit() + "积分；");
                                }
                            }else{
                                if(benefit.getBenefitCredit()>0) {
                                    rechargeBenefitDescList.add("充值"+benefit.getPrice()+"元，赠送"+benefit.getBenefitCredit()+"积分；");
                                }
                            }
                        }
                    }else if(SysConfigConstants.SystemModuleEnum.PETROL.getValue() == benefit.getSystemModule()){
                        //加油
                        if(1 == benefit.getIsDiscount()){
                            if(benefit.getBenefitDiscountRate().compareTo(BigDecimal.ZERO)>0){
                                petrolBenefitDescList.add(benefit.getProductCategoryName()+benefit.getProductName()+"享"+benefit.getBenefitDiscountRate().multiply(BigDecimal.valueOf(10)).setScale(1, BigDecimal.ROUND_HALF_UP).toString()+"折扣；");
                            }
                        }
                        if(1 == benefit.getIsCredit()){
                            if(benefit.getBenefitCreditRate().compareTo(BigDecimal.ZERO)>0){
                                creditBenefitDescList.add(benefit.getProductCategoryName()+benefit.getProductName()+"满100元，赠送"+BigDecimal.valueOf(100).multiply(benefit.getBenefitCreditRate()).intValue()+"积分；");
                            }
                        }
                    }
                }
                one.setPetrolBenefitDescList(petrolBenefitDescList);
                one.setRechargeBenefitDescList(rechargeBenefitDescList);
                one.setCreditBenefitDescList(creditBenefitDescList);
            }
        }
        return vipTypeBenefitList;
    }
}
